package yxy.game.sdk.vivo_quick;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import yxy.apple.http.server.business.BusinessType;
import yxy.apple.logger.Logger;
import yxy.game.pm2.bean.channel.SdkChannel;
import yxy.game.pm2.bean.db.sdk.SDK_UnionChannel;
import yxy.game.pm2.bean.db.sdk.SDK_UnionChannelDAO;
import yxy.game.util.HttpHelper;

import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;

//根据token获取用户信息
@BusinessType(code = "TOKEN")
public class Token extends Sdk_vivo_quick {
    private static final Logger logger = Logger.create(Token.class);

    @Override
    public JSONObject handle(JSONObject request) throws Exception {
        logger.info(SdkChannel.sdk_vivo_quick + ":token:%s", request.toJSONString());
        JSONObject result = new JSONObject();
        result.put("result", 0);
        result.put("message", "");

        try {
            String appId = request.getString("appId");
            String token = request.getString("token");
            SDK_UnionChannel unionChannel = SDK_UnionChannelDAO.getByAppId(SdkChannel.sdk_vivo_quick, appId);
            if (unionChannel == null) throw new Exception("渠道不存在");

            TreeMap<String, String> signMap = new TreeMap<>();
            signMap.put("appKey", unionChannel.getAppKey());
            signMap.put("appSecret", unionChannel.getAppSecret());
            signMap.put("nonce", String.valueOf(System.currentTimeMillis()));
            signMap.put("pkgName", unionChannel.getPkgName());
            signMap.put("timestamp", String.valueOf(System.currentTimeMillis()));
            signMap.put("token", token);
            String content = makeSignContent(signMap); //加密串组合
            logger.info(SdkChannel.sdk_vivo_quick + ":token:content:" + content);
            String sign = DigestUtils.sha256Hex(content); //生成sign

            JSONObject params = new JSONObject();
            params.put("pkgName", unionChannel.getPkgName());
            params.put("token", token);
            params.put("timestamp", signMap.get("timestamp"));
            params.put("nonce", signMap.get("nonce"));
            params.put("signature", sign);
            logger.info(SdkChannel.sdk_vivo_quick + ":token:params:" + JSON.toJSONString(params));
            String url = "https://quickgame.vivo.com.cn/api/quickgame/cp/account/userInfo";
            String res = HttpHelper.doPost(url, HttpHelper.getValuePair(params), 600000); //
            logger.info(SdkChannel.sdk_vivo_quick + ":token:res:" + res);
            JSONObject resobj = JSONObject.parseObject(res);
            if (!resobj.getString("code").equals("0")) throw new Exception(resobj.getString("msg"));
            result.put("data", resobj.get("data"));
        } catch (Exception e) {
            result.put("result", 99);
            result.put("message", "token验证失败:" + e.getMessage());
        }
        return result;
    }

    //生成签名串
    private String makeSignContent (TreeMap<String, String> signMap) {

        Set<String> keys = signMap.keySet();

        StringBuilder signString = new StringBuilder();
        Iterator<String> iterator = keys.iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            signString.append(key).append("=").append(signMap.get(key)).append("&");
        }
        String signatureContent = signString.toString().substring(0, signString.length() - 1);
        return signatureContent;
    }
}
